package org.dromara.solonplugins.zbus.rpc;

import org.noear.solon.Utils;
import org.noear.solon.core.AopContext;
import org.noear.solon.core.Props;

import io.zbus.transport.ServerAddress;

public class ZbusClientBean {
	
	//zbusHttpClientBootstrap
	public io.zbus.rpc.bootstrap.http.ClientBootstrap createHttpClientBootstrap(AopContext context) {
		io.zbus.rpc.bootstrap.http.ClientBootstrap bootstrap = new io.zbus.rpc.bootstrap.http.ClientBootstrap();
		ServerAddress serverAddress = new ServerAddress();
		Props conf = context.cfg();
		String serviceToken = conf.get("zbus.rpc.client.serviceToken");
		if (Utils.isNotBlank(serviceToken)) {
			bootstrap.serviceToken(serviceToken);
			serverAddress.token = serviceToken;
		}
		String clientPoolSize = conf.get("zbus.rpc.client.clientPoolSize");
		if (Utils.isNotBlank(clientPoolSize)) {
			bootstrap.clientPoolSize(Integer.parseInt(clientPoolSize));
		}

		String sa = conf.get("zbus.rpc.client.serverAddress");
		if (Utils.isNotBlank(sa)) {
			serverAddress.address = sa;
		}
		String sslEnabled = conf.get("zbus.rpc.client.sslEnabled");
		if (Utils.isNotBlank(sslEnabled)) {
			serverAddress.sslEnabled = Boolean.parseBoolean(sslEnabled);
		}
		String certificate = conf.get("zbus.rpc.client.certificate");
		if (Utils.isNotBlank(certificate)) {
			serverAddress.certificate = certificate;
		}
		return bootstrap;
	}
	
	//zbusMqClientBootstrap
	public io.zbus.rpc.bootstrap.mq.ClientBootstrap createMqClientBootstrap(AopContext context) {
		io.zbus.rpc.bootstrap.mq.ClientBootstrap bootstrap = new io.zbus.rpc.bootstrap.mq.ClientBootstrap();
		Props conf = context.cfg();
		bootstrap.serviceName(conf.get("zbus.rpc.client.serviceName"));
		String serviceToken = conf.get("zbus.rpc.client.serviceToken");
		if (Utils.isNotBlank(serviceToken)) {
			bootstrap.serviceToken(serviceToken);
		}
		String serviceName = conf.get("zbus.rpc.client.serviceName");
		if (Utils.isNotBlank(serviceName)) {
			bootstrap.serviceName(serviceName);
		}
		bootstrap.broker(context.getBean("zbusBroker"));
		return bootstrap;
	}
	
	

}
